package com.hskn.hss.module.depart.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.hskn.hss.core.annotation.SysLog;
import com.hskn.hss.core.enums.BusinessType;
import com.hskn.hss.entity.AjaxResult;
import com.hskn.hss.exception.HssException;
import com.hskn.hss.module.costdictitem.mapper.CostDictItemMapper;
import com.hskn.hss.module.depart.entity.SysDepart;
import com.hskn.hss.module.depart.mapper.SysDepartMapper;
import com.hskn.hss.module.depart.service.ISysDepartService;
import com.hskn.hss.utils.Tools;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author hskn
 * @since 2021-04-30
 */
@Api(tags = {"-sys_depart"}, description = "“机构管理")
@Slf4j
@RestController
@RequestMapping("/hss/sysdepart")
public class SysDepartController {

    @Autowired
    private ISysDepartService sysDepartService;
    @Resource
    private SysDepartMapper sysDepartMapper;

    @SysLog(businessType = BusinessType.INSERT, tableName = "sys_depart", bean = SysDepart.class, beanMapper = SysDepartMapper.class, names = "departName")
    @RequestMapping(value = "insertDepart", method = RequestMethod.POST)
    public AjaxResult<Object> insertDepart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysDepartService.insert(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.UPDATE, tableName = "sys_depart", idKey = "id", bean = SysDepart.class, beanMapper = SysDepartMapper.class, names = "departName")
    @RequestMapping(value = "updateDepart", method = RequestMethod.POST)
    public AjaxResult<Object> updateDepart(HttpServletRequest request) {
        try {
            JSONObject params = JSON.parseObject(Tools.getBody(request));
            return sysDepartService.update(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("操作失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_depart", idKey = "id", bean = SysDepart.class, beanMapper = SysDepartMapper.class, names = "departName")
    @RequestMapping(value = "deleteDepartById", method = RequestMethod.POST)
    public AjaxResult<Object> deleteDepartById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return sysDepartService.deleteByid(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @SysLog(businessType = BusinessType.DELETE, tableName = "sys_depart", idKey = "ids", bean = SysDepart.class, beanMapper = SysDepartMapper.class, names = "departName")
    @ApiOperation(value = "批量删除接口", notes = "批量删除")
    @ApiResponses({
            @ApiResponse(code = 200, message = "ok", response = SysDepart.class)
    })
    @RequestMapping(value = "deletebatchbyid", method = RequestMethod.POST)
    public AjaxResult<Object> deletebatchbyid(@RequestBody @Validated SysDepart sysDepart) {

        try {
            return sysDepartService.deletebatchbyid(sysDepart);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("删除失败，请稍后再试");
        }
    }

    @RequestMapping(value = "getDepartById", method = RequestMethod.POST)
    public AjaxResult<Object> getDepartById(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        SysDepart SysDepart = null;
        try {
            SysDepart = sysDepartService.getInfoById(params);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }
        return AjaxResult.success(SysDepart);

    }

    @RequestMapping(value = "getDepartList", method = RequestMethod.POST)
    public AjaxResult<Object> getDepartList(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            JSONObject json = sysDepartService.getList(params);
            return AjaxResult.success(json);
        } catch (HssException e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败，请稍后再试");
        }

    }

    @RequestMapping(value = "getTreeList", method = RequestMethod.POST)
    public AjaxResult<Object> getTreeList() {
        return AjaxResult.success(sysDepartService.getTreeList());
    }

    @RequestMapping(value = "getAppUserList", method = RequestMethod.POST)
    public AjaxResult<Object> getAppUserList() {
        try {
            return AjaxResult.success(sysDepartService.getAppUserList());
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败");
        }
    }

    @RequestMapping(value = "getAppUserListBySearch", method = RequestMethod.POST)
    public AjaxResult<Object> getAppUserListBySearch(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            return AjaxResult.success(sysDepartService.getAppUserListBySearch(params));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败");
        }
    }
    @RequestMapping(value = "selectDepartAlarmCharsTale", method = RequestMethod.POST)
    public AjaxResult<Object> selectDepartAlarmCharsTale(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            SysDepart depart=new SysDepart();
            if(params!=null){
                depart=JSONObject.parseObject(params.toJSONString(),SysDepart.class);
            }
            if (!params.containsKey("pageSize")||!params.containsKey("pageNum")) {
              return AjaxResult.fail("pageNum,pageSize传输失败");
            }
            return sysDepartService.selectDepartAlarmCharsTale(params.getInteger("pageNum"),
                    params.getInteger("pageSize"),depart);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败");
        }
    }
    @RequestMapping(value = "selectDepartAlarmChars", method = RequestMethod.POST)
    public AjaxResult<Object> selectDepartAlarmChars(HttpServletRequest request) {
        JSONObject params = JSON.parseObject(Tools.getBody(request));
        try {
            SysDepart depart=new SysDepart();
            if(params!=null){
                depart=JSONObject.parseObject(params.toJSONString(),SysDepart.class);
            }
            return sysDepartService.selectDepartAlarmChars(depart);
        } catch (Exception e) {
            e.printStackTrace();
            log.error("报错ERROR", e);
            return AjaxResult.fail("获取信息失败");
        }
    }
//    @PostMapping(value = "getIdsById")
//    public AjaxResult getIdsById(@RequestParam(name = "id",required = true) String id) {
//        try {
//            QueryWrapper queryWrapper = new QueryWrapper();
//            List<SysDepart> sysDepartList = sysDepartMapper.selectList(queryWrapper);
//            Map<String, List<String>> childListMap = new HashMap<>();
//            sysDepartList.forEach(t->{
//                childListMap.put(t.getId(), new ArrayList<>());
//                for (SysDepart depart : sysDepartList) {
//                    if (depart.getParentId().equals(t.getId())){
//                        childListMap.get(t.getId()).add(depart.getId());
//                    }
//                }
//            });
//
//        } catch (Exception e) {
//            e.printStackTrace();
//            log.error("报错ERROR", e);
//            return AjaxResult.fail("获取信息失败");
//        }
//    }
}

